﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyHocPhi.Model;
using QuanLyHocPhi.BLL;

namespace QuanLyHocPhi.GUI.DuLieu
{
    public partial class SelectHocKyDialog : QuanLyHocPhi.GUI.FormBaseDialog
    {
        private bool m_UseUserData;
        private string m_MaHocKyToExclude;
        
        /// <summary>
        /// Hàm khởi dựng
        /// </summary>
        public SelectHocKyDialog(bool useUserData = false, string maHocKyToExclude = null)
        {
            InitializeComponent();

            gridHocKy.AutoGenerateColumns = false;
            gridHocKy.AllowUserToAddRows = false;
            gridHocKy.AllowUserToDeleteRows = false;
            gridHocKy.ReadOnly = true;
            colHocKy_NamHoc.DataPropertyName = "NamHoc";
            colHocKy_SoHieuHocKy.DataPropertyName = "SoHieuHocKy";
            colHocKy_TrangThaiHoatDong.Visible = false;
            m_UseUserData = useUserData;
            m_MaHocKyToExclude = maHocKyToExclude;

            NapDanhSachNamHoc();
        }

        public HocKy SelectedHocKy
        {
            get
            {
                if (bsHocKy.DataSource == null || bsHocKy.Current == null)
                    return null;
                else
                    return bsHocKy.Current as HocKy;
            }
        }

        private void NapDanhSachNamHoc()
        {
            this.m_LockUpdate = true;
            bsNamHoc.CurrentChanged -= OnBsNamHocCurrentChanged;
            bsNamHoc.DataSource = BizHocKy.GetList_NamHoc();
            cbxNamHoc.DataSource = bsNamHoc;
            if (m_UseUserData)
            { 
                // Đọc thông tin năm học được lựa chọn cuối cùng từ registry
                string namHoc = Registry.GetStringValue(Registry.Key_NamHoc);
                if (namHoc.IsNullOrEmpty() == false)
                    try
                    {
                        cbxNamHoc.SelectedItem = namHoc;
                    }
                    catch { }
            }
            bsNamHoc.CurrentChanged += OnBsNamHocCurrentChanged;
            this.m_LockUpdate = false;
            HienThiDanhSachHocKy();
        }

        void OnBsNamHocCurrentChanged(object sender, EventArgs e)
        {
            HienThiDanhSachHocKy();
        }

        private void HienThiDanhSachHocKy()
        {
            if (m_LockUpdate)
                return;
            int oldPosition = 0;
            bsHocKy.CurrentChanged -= OnBsHocKyCurrentChanged;
            string selectedNamHoc = null;
            if (bsNamHoc.Current != null)
                selectedNamHoc = bsNamHoc.Current.ToString();
            if (selectedNamHoc != null)
            {
                var lstHocKy = BizHocKy.GetList_HocKy(selectedNamHoc);
                bsHocKy.DataSource = lstHocKy;
                if (m_UseUserData)
                {
                    string stSoHieuHocKy = Registry.GetStringValue(Registry.Key_HocKy);
                    if (stSoHieuHocKy.IsNullOrEmpty() == false)
                    {
                        try
                        {
                            int soHieuHocKy = Convert.ToInt16(stSoHieuHocKy);
                            for (int i = 0; i < lstHocKy.Count; i++)
                                if (lstHocKy[i].SoHieuHocKy == soHieuHocKy)
                                {
                                    oldPosition = i;
                                    break;
                                }
                        }
                        catch { }
                    }
                }
            }
            else
                bsHocKy.DataSource = null;
            
            colHocKy_TrangThaiHoatDong.Visible = false;
            gridHocKy.AutoGenerateColumns = false;
            gridHocKy.DataSource = bsHocKy;
            bsHocKy.Position = oldPosition;
            bsHocKy.CurrentChanged += OnBsHocKyCurrentChanged;            
            UpdateStatusOfSelectedHocKy();
        }

        private void OnCbxNamHocSelectedIndexChanged(object sender, EventArgs e)
        {
            HienThiDanhSachHocKy();
        }

        private void OnOKClick(object sender, EventArgs e)
        {
            HocKy selectedHocKy = this.SelectedHocKy;
            if (selectedHocKy != null && m_UseUserData)
            {
                Registry.SetValue(Registry.Key_NamHoc, selectedHocKy.NamHoc);
                Registry.SetValue(Registry.Key_HocKy, selectedHocKy.SoHieuHocKy);
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void UpdateStatusOfSelectedHocKy()
        {
            HocKy selectedHocKy = this.SelectedHocKy; ;
            btnOK.Enabled = (selectedHocKy != null && selectedHocKy.MaHocKy != m_MaHocKyToExclude);
        }

        private void OnBsHocKyCurrentChanged(object sender, EventArgs e)
        {
            UpdateStatusOfSelectedHocKy();
        }
    }
}
